//
//  KABCosplayReplyVC.m
//  kabu
//
//  Created by XiangDongMBP on 2017/3/22.
//  Copyright © 2017年 East. All rights reserved.
//

#import "KABCosplayReplyVC.h"
#import <YYKeyboardManager.h>
#import "UIColor+XDHex.h"
#import "SZTextView.h"

@interface KABCosplayReplyVC ()<YYKeyboardObserver, UITextViewDelegate>

@property (weak, nonatomic) IBOutlet UIView *inputBgView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *inputBgViewBottomConstraint;
@property (weak, nonatomic) IBOutlet SZTextView *textView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *textViewHeightConstraint;
@property (weak, nonatomic) IBOutlet UIButton *sendBtn;

@end

static NSInteger const kTextViewMaxHeight = 100.0;
static NSInteger const kTextViewMaxStrLength = 100.0;

@implementation KABCosplayReplyVC

#pragma mark - life cycle
- (void)dealloc {
    [[YYKeyboardManager defaultManager] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [[YYKeyboardManager defaultManager] addObserver:self];
    
    [self initNavigationBar];
    [self initSubViews];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.textView becomeFirstResponder];
}

- (void)initNavigationBar {
    
}

- (void)initSubViews {
    self.textView.layer.borderWidth = 1;
    self.textView.layer.borderColor = [UIColor xd_colorWithHex:0xE7E7E7].CGColor;
    self.textView.layer.masksToBounds = YES;
    self.textView.layer.cornerRadius = 4;
    self.textView.text = self.preString;
    self.textView.placeholder = self.placeHolder ? self.placeHolder : @"输入评论";
    self.textView.delegate = self;
    [self textViewDidChange:self.textView];
    
    [self.sendBtn addTarget:self action:@selector(sendBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    [self backToSuperVCWithHandle:NO];
}

#pragma mark - @protocol YYKeyboardObserver
- (void)keyboardChangedWithTransition:(YYKeyboardTransition)transition {
    [UIView animateWithDuration:transition.animationDuration delay:0 options:transition.animationOption animations:^{
        CGRect kbFrame = [[YYKeyboardManager defaultManager] convertRect:transition.toFrame toView:self.view];
        CGRect textframe = self.inputBgView.frame;
        textframe.origin.y = kbFrame.origin.y - textframe.size.height;
        self.inputBgView.frame = textframe;
        CGFloat height = ([UIScreen mainScreen].bounds.size.height - kbFrame.origin.y);
        self.inputBgViewBottomConstraint.constant = height;
    } completion:^(BOOL finished) {
        ;
    }];
}

#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView {
    NSInteger maxLength = self.maxStrLength ? self.maxStrLength : kTextViewMaxStrLength;
    NSString *toBeString = textView.text;
    //获取高亮部分
    UITextRange *selectedRange = [textView markedTextRange];
    UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
    // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
    if (!position || !selectedRange) {
        if (toBeString.length > maxLength) {
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:maxLength];
            if (rangeIndex.length == 1) {
                textView.text = [toBeString substringToIndex:maxLength];
            } else {
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, maxLength)];
                textView.text = [toBeString substringWithRange:rangeRange];
            }
        }
    }
    
    // 动态改变高度
    CGFloat height = [textView sizeThatFits:CGSizeMake(textView.bounds.size.width, MAXFLOAT)].height;
    if (height > kTextViewMaxHeight) {
        textView.scrollEnabled = YES;
    } else {
        textView.scrollEnabled = NO;
        self.textViewHeightConstraint.constant = height;
    }
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]){
        [self backToSuperVCWithHandle:YES];
        return NO;
    }
    return YES;
}

#pragma mark - event response
- (void)sendBtnTapped:(UIButton *)btn {
    [self backToSuperVCWithHandle:YES];
}

#pragma mark - private methods
- (void)backToSuperVCWithHandle:(BOOL)withHandle {
    [self.textView resignFirstResponder];
    NSString *replyStr = self.textView.text;
    [self dismissViewControllerAnimated:YES completion:^{
        if (withHandle) {
            if (self.completeHandle) {
                self.completeHandle(replyStr);
                self.completeHandle = nil;
            }
        }
    }];
}

#pragma mark - network request
#pragma mark - getters and setters

@end
